//
//  ParametersCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/24/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


class ParametersCell: UITableViewCell, ConfigurableCell, ParametersViewDelegate, OptionCellProtocol {
    
    lazy var parameterView: ParametersView = {
       let view = ParametersView()
       view.delegate = self
       return view
    }()
    
    var delegate: OptionCellActionProtocol!
    private var index: Int!
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(parameterView)
        self.contentView.backgroundColor = UIColor(red: 244/255, green: 246/255, blue: 250/255, alpha: 1)
        
        parameterView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.bottom.equalToSuperview().inset(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(data object: CarFilterModel, index: Int) {
        self.index = index
        let parameters = object.inputs.map {
            x in ParametersViewModel(input: x)
        }
        parameterView.items = parameters
    }
    
    func returnData() -> CarFilterModel {
        let carFilterModel = CarFilterModel(carFilter: CarFilter(marka: nil, models: []))
        return carFilterModel
    }
    
    func didSelectParameter(item: ParametersViewModel) {
        let _item = item
        _item.value = parameterView.items[0].value
        delegate.selectMarkaModel(index: index!, item: _item)
    }
    
    func didClearValue(value: String?, placeholder: String, itemIndex: Int) {
        delegate.clearParameterValue(value: value, placeholder: placeholder, index: index, itemIndex: itemIndex)
    }
}
